<?php

class FileUtil {

    public static function getImageType($filename) {
        $image_info = getimagesize($filename);
        $type = $image_info['mime'];
        if ($type == "image/gif")
            return '.gif';
        else if ($type == "image/png")
            return '.png';
        else
            return '.jpg';  //default type is IMAGETYPE_JPEG
    }

    public static function getImageProfile($img) {
        $image_info = getimagesize($img);
        $type_code = -1;

        if ($image_info['mime'] == 'image/gif') {
            $type_code = IMAGE_EXT_GIF;
        } else if ($image_info['mime'] == 'image/jpeg') {
            $type_code = IMAGE_EXT_JPEG;
        } else if ($image_info['mime'] == 'image/png') {
            $type_code = IMAGE_EXT_PNG;
        }

        return array(
            'width' => $image_info[0],
            'height' => $image_info[1],
            'type' => $type_code,
            'size' => filesize($img)
        );
    }

    public static function deleteFolder($dir) {
        if (is_dir($dir)) {
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ($object != "." && $object != "..") {
                    if (filetype($dir . "/" . $object) == "dir")
                        deleteFolder($dir . "/" . $object); 
                    else
                        unlink($dir . "/" . $object);
                }
            }
            reset($objects);
            rmdir($dir);
        }
    }

}